<template>
   <div class="login_container">
      <el-row>
         <el-col :span="12" :xs="0"></el-col>
         <el-col :span="12" :xs="24">
            <el-form class="login_form" :model="loginFormData" :rules="rules" ref="loginForm">
               <h1>Hello</h1>
               <h2>欢迎来到硅谷甄选</h2>
               <el-form-item class="login_item" prop="username">
                  <el-input :prefix-icon="User" v-model="loginFormData.username" placeholder="用户名"></el-input>
               </el-form-item>
               <el-form-item class="login_item" prop="password">
                  <el-input type="password" :prefix-icon="Lock" v-model="loginFormData.password" show-password
                     placeholder="密码"></el-input>
               </el-form-item>
               <el-form-item class="login_item">
                  <el-button class="login_btn" type="primary" @click="login" :loading="loading">登录</el-button>
               </el-form-item>
            </el-form>
         </el-col>
      </el-row>
   </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
// 引入用户相关的小仓库
import useUserStore from '@/store/modules/user'
import { ElNotification } from 'element-plus'
// 引入获取当前时间的函数
import { getTime } from '@/utils/time'
let userStore = useUserStore()
// 获取el-form组件
let loginForm = ref()
// 获取路由器
let $router = useRouter()
// 获取路由对象
let $route = useRoute()
// 收集表单数据
let loginFormData = reactive({ username: 'admin', password: 'atguigu123' })
// 定义变量控制按钮加载效果
let loading = ref(false)
// 登录按钮的回调
const login = async () => {
   // 保证全部表单规则校验通过再发送请求
   await loginForm.value.validate()
   // 点击登录页面以后干什么？
   // 通知仓库发起登录请求
   // 请求成功->跳转到首页
   // 请求失败->弹出请求失败信息
   // 开始加载
   loading.value = true
   try {
      // 登录成功
      await userStore.userLogin(loginFormData)
      // 判断登录时，路由路径中是否有query参数
      let redirect: any = $route.query.redirect
      if (redirect) {
         // 有：往query参数跳转
         $router.push(redirect)
      } else {
         // 没有：跳转到首页
         $router.push('/')
      }
      // 登录成功提示信息
      ElNotification({
         type: 'success',
         message: '欢迎回来',
         title: `HI,${getTime()}好`
      })
      // 登录成功加载效果也消失
      loading.value = false
   } catch (error) {
      // 登录失败的提示信息
      ElNotification({
         type: 'error',
         message: (error as Error).message
      })
      // 登录失败加载效果消失
      loading.value = false
   }
}
// 自定义校验规则函数
const validatorUserName = (rule: any, value: any, callback: any) => {
   // rule:即为校验规则对象
   // value:即为表单元素文本内容
   // 函数:如果符合校验，调用callback函数放行，如果校验不通过，则同样调用callback且注入错误信息
   if (value.length >= 5) {
      callback()
   } else {
      callback(new Error('账号长度至少五位'))
   }
}
const validatorPassword = (rule: any, value: any, callback: any) => {
   if (value.length >= 6) {
      callback()
   } else {
      callback(new Error('密码长度至少六位'))
   }
}
// 定义表单校验需要配置对象
const rules = {
   username: [
      // { required: true, message: '用户名不能为空！', trigger: 'blur' },
      // { min: 5, message: '账号长度至少五位！', trigger: 'change' }
      // 自定义校验规则
      { validator: validatorUserName, trigger: 'change' }
   ],
   password: [
      // { required: true, message: '密码不能为空！', trigger: 'blur' },
      // { min: 6, max: 15, message: '密码长度为6-15位！', trigger: 'change' }
      { validator: validatorPassword, tigger: 'change' }
   ]
}
</script>

<style lang="scss" scoped>
.login_container {
   width: 100%;
   height: 100vh;
   background: url('@/assets/images/background.jpg') no-repeat;
   background-size: cover;

   .login_form {
      width: 80%;
      position: relative;
      top: 30vh;
      background: url('@/assets/images/login_form.png');
      padding: 40px;

      h1 {
         color: white;
         font-size: 40px;
      }

      h2 {
         color: white;
         font-size: 20px;
         margin: 20px 0;
      }

      .login_item {
         padding: 0 40px;

         .login_btn {
            width: 100%;
         }
      }


   }
}
</style>